package com.zr.wareHouseIdentify.Service.ServiceImpl;

import com.zr.meiju.StatusEnum;
import com.zr.util.AllRecords;
import com.zr.util.ResultBuildVo;
import com.zr.util.ResultVo;
import com.zr.wareHouseIdentify.Mapper.WareHouseMapper;
import com.zr.wareHouseIdentify.Service.WareHouseService;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentify;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentifyAdd;
import com.zr.wareHouseIdentify.pojo.WareHouseIdentifyUpdate;
import com.zr.wareHouseIdentify.pojo.WareHouseStatusVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by jiangping on 2019/7/16.
 */
@Service
public class WareHouseServiceImpl implements WareHouseService{
    @Autowired
    private WareHouseMapper wareHouseMapper;

    @Override
    public ResultVo addWarehouse( WareHouseIdentifyAdd wareHouseIdentifyAdd) {

        ResultVo assigment = assigment(wareHouseIdentifyAdd);
        if (!assigment.isSuccess()){
            return assigment;
//            return ResultBuildVo.error(assigment.getErrorMessage(),assigment.getErrorCode());
        }
        //给数据库中的创建时间和修改时间等信息赋值
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();
        wareHouseIdentify.setCreateTime(nowDate);
        wareHouseIdentify.setCreateName("小明");
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(wareHouseIdentifyAdd,wareHouseIdentify);


        wareHouseMapper.addWarehouse(wareHouseIdentifyAdd);
        return ResultBuildVo.success(wareHouseIdentifyAdd);
    }
    public ResultVo assigment(WareHouseIdentifyAdd wareHouseIdentifyAdd){
        //        1.验证编码是否已经存在
        List<WareHouseIdentify> subClassesCode = wareHouseMapper.queryByCodes(wareHouseIdentifyAdd.getCode());
        if (!CollectionUtils.isEmpty(subClassesCode)){
            return ResultBuildVo.error("已经存在编码！","500");
        }
//        1.验证名称是否已经存在
        List<WareHouseIdentify> subClassesName = wareHouseMapper.queryByNames(wareHouseIdentifyAdd.getName());
        if (!CollectionUtils.isEmpty(subClassesName)){
            return ResultBuildVo.error("已经存在名称！","500");
        }
        return ResultBuildVo.success(wareHouseIdentifyAdd);
    }
    @Override
    public ResultVo queryPage(WareHouseIdentify wareHouseIdentify) {
        List<WareHouseIdentify> wareHouseIdentifyList = wareHouseMapper.queryPage(wareHouseIdentify);

        int count = wareHouseMapper.queryCount(wareHouseIdentify);
        AllRecords allRecords = new AllRecords();
        allRecords.setTotalNumber(count);
        allRecords.setPageSize(wareHouseIdentify.getPageSize());
        allRecords.setPageIndex(wareHouseIdentify.getPageIndex());
        allRecords.setDataList(wareHouseIdentifyList);
        allRecords.resetTotalNumber(count);

        return ResultBuildVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo updateStatus( WareHouseStatusVo wareHouseStatusVo) {
        //逻辑验证
        //验证数据是否存在
        List<WareHouseIdentify> queryPlantById = wareHouseMapper.queryById(wareHouseStatusVo.getId());

        if (queryPlantById==null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //给数据库中的创建时间和修改时间等信息赋值
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("小明");
        //把左边对象的数据赋值给右边对象的数据
        //注意必须相同参数名称和相同参数类型才能自动进行赋值
        BeanUtils.copyProperties(wareHouseStatusVo, wareHouseIdentify);
        wareHouseMapper.updateStatus(wareHouseIdentify);
        return ResultBuildVo.success(wareHouseIdentify);
    }
    @Override
    @Transactional
    public ResultVo UpdateWarehouse(@Valid WareHouseIdentifyUpdate wareHouseIdentifyUpdate) {
        //判断是否为禁用，禁用可以修改
        ResultVo assigment = assigmentUpdate(wareHouseIdentifyUpdate);
        if (!assigment.isSuccess()) {
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("小明");
        BeanUtils.copyProperties(wareHouseIdentifyUpdate, wareHouseIdentify);
        wareHouseMapper.UpdateWarehouse(wareHouseIdentifyUpdate);
        return ResultBuildVo.success(wareHouseIdentifyUpdate);
    }
    private ResultVo assigmentUpdate(WareHouseIdentifyUpdate wareHouseIdentifyUpdate) {
        //验证数据是否存在
        List<WareHouseIdentify> wareHouseIdentifies = wareHouseMapper.queryById(wareHouseIdentifyUpdate.getId());
        if (CollectionUtils.isEmpty(wareHouseIdentifies)){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        //禁用状态下才允许修改
        if (wareHouseIdentify.getStatus()== StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }
        return ResultBuildVo.success(wareHouseIdentifyUpdate);
    }

}
